package com.chainyoung.component_tmperson.ui.mvp.presenter;

import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.di.scope.ActivityScope;
import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.component_tmperson.entity.CoinItem;
import com.chainyoung.component_tmperson.ui.mvp.contract.AddPurseContract;


import java.util.List;
import java.util.TreeMap;

import javax.inject.Inject;

import static com.chainyoung.common.constant.LibConstant.NO_RESULT_ERRORCODE;


@ActivityScope
public class AddPursePresenter  extends BasePresenter<AddPurseContract.Model, AddPurseContract.View> {

    @Inject
    public AddPursePresenter(AddPurseContract.Model model, AddPurseContract.View rootView) {
        super(model, rootView);
    }


    public void addPurseAddr(TreeMap<String, Object> map) {
        addSubscribe(mModel.addPurseAddr(map)
                .compose(RxUtils.<String>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<String>(mView,true) {
                    @Override
                    public void onNext(String data) {
                        mView.onAddPurseAddrSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }


    public void getCoinList(TreeMap<String, Object> map) {
        addSubscribe(mModel.getCoinList(map)
                .compose(RxUtils.<List<CoinItem>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<CoinItem>>(mView) {
                    @Override
                    public void onNext(List<CoinItem> data) {
                        mView.onGetCoinListSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)) {
                                mView.onGetCoinListSuccess(null);
                                return;
                            }

                        }
                        super.onError(e);
                    }
                }));
    }



}



